package com.xiaoma.weblog.web.controller;

import com.xiaoma.weblog.common.aspect.ApiOperationLog;
import com.xiaoma.weblog.common.enums.ResponseCodeEnum;
import com.xiaoma.weblog.common.exception.BizException;
import com.xiaoma.weblog.common.utils.JsonUtil;
import com.xiaoma.weblog.common.utils.Response;
import com.xiaoma.weblog.web.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName TestController
 * @Description: 测试控制器
 * @Author: 小马
 * @CreateDate: 2025/3/26 17:55
 * @Version: 1.0
 */
@RestController
@Slf4j
@Api(tags = "测试控制器")
public class TestController {

    @PostMapping("/test")
    @ApiOperationLog(description = "测试接口")
    @ApiOperation("测试接口")
    public User test(@RequestBody User user) {
        // 返参
        return user;
    }

    @PostMapping("/test1")
    @ApiOperationLog(description = "测试接口1")
    public ResponseEntity<String> test1(@RequestBody @Validated User user, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage)
                    .collect(Collectors.joining(", "));
            return ResponseEntity.badRequest().body(errorMsg);
        }
        return ResponseEntity.ok("参数没有问题");
    }

    @PostMapping("/test2")
    @ApiOperationLog(description = "测试接口2")
    public Response<Object> test2(@RequestBody @Validated User user, BindingResult bindingResult) {
        if(bindingResult.hasErrors()) {
            String errorMsg = bindingResult.getFieldErrors().stream().map(FieldError::getDefaultMessage)
                    .collect(Collectors.joining(", "));
            return Response.fail(errorMsg);
        }
        return Response.success();
    }

    @PostMapping("/test3")
    @ApiOperationLog(description = "测试接口3")
    public Response<Object> test3() {
        throw new BizException(ResponseCodeEnum.PRODUCT_NOT_FOUND);
    }

    @PostMapping("/test4")
    @ApiOperationLog(description = "测试接口4")
    public Response<Object> test4(@RequestBody @Validated User user) {
        // 打印入参
        log.info(JsonUtil.toJsonString(user));

        // 设置三种日期字段值
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDate.now());
        user.setTime(LocalTime.now());

        return Response.success(user);
    }

    @PostMapping("/admin/test5")
    @ApiOperationLog(description = "测试接口5")
    public Response<Object> test5(@RequestBody @Validated User user) {
        // 打印入参
        log.info(JsonUtil.toJsonString(user));

        // 设置三种日期字段值
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDate.now());
        user.setTime(LocalTime.now());

        return Response.success(user);
    }

    @PostMapping("/admin/update")
    @ApiOperationLog(description = "测试更新接口（权限）")
    @ApiOperation(value = "测试更新接口（权限）")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response<Object> test6(@RequestBody @Validated User user) {
        log.info("更新成功..........");
        return Response.success();
    }
}